﻿using System;
using System.Xml.Linq;
using Engine;
using TemplatesDatabase;
using Game;

namespace HYKJ
{
    public class SubsystemCupBlockBehavior : SubsystemBlockBehavior
    {
        public ComponentPlayer m_componentPlayer;

        public SubsystemAudio m_subsystemAudio;

        public SubsystemParticles m_subsystemParticles;

        public SubsystemGameInfo m_subsystemGameInfo;

        public const string fName = "SubsystemCupBlockBehavior";

        public override int[] HandledBlocks => new int[1]
        {
            BlocksManager.GetBlockIndex<CupBlock>(),
        };

        public override void Load(ValuesDictionary valuesDictionary)
        {
            base.Load(valuesDictionary);
            this.m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(true);
            this.m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(true);
            this.m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(true);
        }

        public override bool OnUse(Ray3 ray, ComponentMiner componentMiner)
        {
            IInventory inventory = componentMiner.Inventory;
            int activeBlockValue = componentMiner.ActiveBlockValue;
            int num = Terrain.ExtractContents(activeBlockValue);

            if (num == CupBlock.Index)
            {
                object obj = componentMiner.Raycast(ray, RaycastMode.Gathering, true, true, true);
                if (obj is TerrainRaycastResult)
                {
                    CellFace cellFace = ((TerrainRaycastResult)obj).CellFace;
                    int cellValue = base.SubsystemTerrain.Terrain.GetCellValue(cellFace.X, cellFace.Y, cellFace.Z);
                    int num2 = Terrain.ExtractContents(cellValue);
                    int num3 = Terrain.ExtractData(cellValue);

                    Block block = BlocksManager.Blocks[num2];
                    if (block is WaterBlock)
                    {
                        int value = Terrain.ReplaceContents(activeBlockValue, BlocksManager.GetBlockIndex<SeaWaterCupBlock>());
                        inventory.RemoveSlotItems(inventory.ActiveSlotIndex, inventory.GetSlotCount(inventory.ActiveSlotIndex));
                        if (inventory.GetSlotCount(inventory.ActiveSlotIndex) == 0)
                        {
                            inventory.AddSlotItems(inventory.ActiveSlotIndex, value, 1);
                            m_subsystemAudio.PlaySound("Audio/Footsteps/Water/FootstepWater2", 1f, 0f, ray.Position, 2f, autoDelay: true);
                            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage(LanguageControl.Get(fName, 1), Color.White, true, true);
                            TerrainRaycastResult? terrainRaycastResult = componentMiner.Raycast<TerrainRaycastResult>(ray, RaycastMode.Interaction);
                            Vector3 hitPosition = terrainRaycastResult.Value.HitPoint();
                            m_subsystemParticles.AddParticleSystem(new WaterSplashParticleSystem(SubsystemTerrain, hitPosition, false));
                        }
                    }
                }
            }
            return true;
        }
    }
}